var canSumb = false;
var ajax = function(method, url, cb, data, dataType) {
    var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
    xhr.open(method.toUpperCase(), url, true);
    if (method.toLowerCase() == 'get') {
        xhr.send(null);
    } else {
        var contentType = 'application/json';
        if (dataType) {
            if (dataType.toLowerCase() == 'json') {
                contentType = 'application/json'
            };
        };
        xhr.setRequestHeader("Content-type", contentType);
        xhr.send(data);
    };
    xhr.onreadystatechange = function() {
        if (xhr.readyState === 4) {
            if (xhr.status === 500 || xhr.status===404){
                window.location.href = '../../templates/error.html';
            };
            if (xhr.status === 200) {
                cb(xhr.responseText);
            };
        };
    };
};
var testSumb = function(){
    if (q('.zkzh').value.length == 15 && q('.name input').value.length != 0) {
        canSumb = true;
        return true;
    };
    if(q('.zkzh').value.length != 15){
        q('.zkzhErr').style.display = 'block';
        canSumb = false;
        return false;
    };
    if(q('.name input').value.length == 0){
        q('.nameErr').style.display = 'block';
        canSumb = false;
        return false;
    };
};
var q = function(css) {
    return document.querySelector(css);
};
var back = function() {
    q('.query').style.display = 'block';
    q('.aftermsg').style.display = 'none';
}
var useMsg = function(data) {
    var jsonData = JSON.parse(data);
    console.log(jsonData);
    if (jsonData.status == 'OK') {
        q('.wait').style.display = 'none';
        q('.aftermsg').style.display = 'block';
        q('.xm').innerHTML = jsonData.xm;
        q('.xx').innerHTML = jsonData.xx;
        q('.afzkzh').innerHTML = jsonData.zkzh;
        q('.kssj').innerHTML = jsonData.kssj;
        q('.zf').innerHTML = jsonData.zf;
        q('.tl').innerHTML = jsonData.tl;
        q('.yd').innerHTML = jsonData.yd;
        q('.xzfy').innerHTML = jsonData.xzfy;
        q('.kslb').innerHTML = jsonData.kslb;
    } else if (jsonData.status == '404' || jsonData.status == '500') {
        window.location.href = '../../templates/error.html';
    } else if(jsonData.status == 'QueryFail'){
        console.log('QueryFail');
        q('.errMsg').innerHTML='用户太多，请稍后重试';
        q('.errMsg').style.display = 'block';
        back();
        canSumb = false;
    }else if(jsonData.status == 'NotFound'){
        console.log('NotFound');
        q('.errMsg').innerHTML='查询失败，请确认输入信息正确';
        q('.errMsg').style.display = 'block';
        back();
        canSumb = false;
    }else if(jsonData.status == 'GetUserInfoError'){
        console.log('GetUserInfoError');
        q('.errMsg').innerHTML='信息不足，查询失败';
        q('.errMsg').style.display = 'block';
        back();
        canSumb = false;
    }else {
        console.log('err');
        q('.errMsg').innerHTML='查询失败，请稍后重试';
        q('.errMsg').style.display = 'block';
        back();
        canSumb = false;
    }
}
var query = function() {
    testSumb();
    if (canSumb) {
        var data = {
            xm: q('.name input').value,
            zkzh: q('.zkzh').value
        }
        var jsonData = JSON.stringify(data);
        console.log(jsonData);
        ajax('post', '/cet/query', useMsg, jsonData);
        q('.errMsg').style.display = 'none';
        q('.query').style.display = 'none';
        q('.wait').style.display = 'block';
    }
};
var testZkzh = function() {
    if (q('.zkzh').value.length != 15) {
        q('.zkzhErr').style.display = 'block';
        canSumb = false;
    } else {
        q('.zkzhErr').style.display = 'none';
        if (q('.name input').value.length != 0) {
             canSumb = true;
        }
    }
};
var testName = function() {
    if (q('.name input').value.length == 0) {
        q('.nameErr').style.display = 'block';
        canSumb = false;
    } else {
        q('.nameErr').style.display = 'none';
        if (q('.zkzh').value.length == 15) {
             canSumb = true;
        }
    }
};
q('.zkzh').addEventListener('blur', testZkzh, false);
q('.name input').addEventListener('blur', testName, false);
q('.subm1 span').addEventListener('click', query, false);
q('.subm3 span').addEventListener('click', back, false);
